package org.example.demo4;

import lombok.extern.slf4j.Slf4j;

import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Gong.Yang
 * 弱引用使用
 */
@Slf4j
public class WeakReferenceDemo {
    static final int _4MB = 1024 * 1024 * 4;
    public static void main(String[] args) {
        List<WeakReference<byte[]>> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            list.add(new WeakReference<>(new byte[_4MB]));
        }
        log.info("【内容查看】 gc 前");
        for (WeakReference<byte[]> weakReference : list) {
            byte[] bytes = weakReference.get();
            System.out.println(bytes);
        }

        log.info("【Full GC】");
        System.gc();

        log.info("【内容查看】 gc 后");
        for (WeakReference<byte[]> weakReference : list) {
            byte[] bytes = weakReference.get();
            System.out.println(bytes);
        }

    }
}
